package com.atguigu4.connection;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import org.junit.Test;

import java.sql.Connection;

/**
 *  C3P0数据库连接池
 * @auther user
 * @date 2021/12/10 - 3:38
 */
public class C3P0Test {
    //方式一
    @Test
    public void testGetConnection() throws Exception {
        //获取C3P0数据库连接池
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
        cpds.setJdbcUrl( "jdbc:mysql://8.130.54.82:3306/test" );
        cpds.setUser("root");
        cpds.setPassword("hwychu1998917Aa");
        //设置相关参数
        //设置数据库连接池初始连接数
        cpds.setInitialPoolSize(5);
        Connection connection = cpds.getConnection();
        System.out.println("connection = " + connection);
        //销毁C3P0连接池，一般不会关
        DataSources.destroy( cpds );
    }
    //方式二：使用c3p0-config.xml配置文件
    @Test
    public void testGetConnection1() throws Exception{
        ComboPooledDataSource cpds = new ComboPooledDataSource("C3P0Config");
        Connection connection = cpds.getConnection();
        System.out.println("connection = " + connection);
        //销毁C3P0连接池，一般不会关
        DataSources.destroy( cpds );
    }
}
